<template>
  <div>
    <div style="height: 50px;text-align: left">
      <el-form  :inline="true" :model="fromInline" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="formInline.name" placeholder="请输入医师姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="query" type="primary">查询</el-button>
          <el-button @click="adddialog" type="primary">添加</el-button>
          <el-button @click="cgOne" type="primary">修改选中的数据</el-button>
          <el-button @click="delSome" type="primary">删除选中的数据</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table
        stripe
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%;line-height: 40px"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="name"
          label="医师姓名"
          width="160">
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄"
          width="160">
      </el-table-column>
      <el-table-column
          prop="sex"
          label="性别"
          width="160"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="level_id"
          label="级别"
          width="160"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="phone"
          label="手机号"
          width="160"
          show-overflow-tooltip>
      </el-table-column>
      <el-table-column
          prop="treatType"
          label="诊治类别"
          width="160"
          show-overflow-tooltip>
      </el-table-column>

      <el-table-column  label="操作" width="120">
        <template slot scope="scope">
          <el-button type="text" size="small" @click="delOneRow(scope.row)">删除</el-button>
          <el-button type="text" size="small" @click="editdialog(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="">重置密码</el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <el-dialog  :visible.sync="addDialogFormVisible" width="40%">
      <AddDT></AddDT>
    </el-dialog>
    <el-dialog  :visible.sync="editDialogFormVisible" width="40%">
      <EditDT :myForm="editForm" ref="edit" @closeEditDialog="closeParentEditDialog"></EditDT>
    </el-dialog>
  </div>

</template>

<script>
import {doctorList,addDoctor,editDoctor} from "@/api/doctor";
import AddDT from "@/views/doctor/AddDT.vue";
import EditDT from "@/views/doctor/EditDT.vue";

export default {
  components: {AddDT,EditDT},
  data() {
    return{
      tableData:[],
      multipleSelection: [],
      pageNum:1,
      pageSize:8,
      pageSizes:[8,16,24],
      total:0,
      formInline:{
        name:''
      },
      addDialogFormVisible: false,
      editDialogFormVisible:false,
      editForm:{}
    }
  },
  created() {
    this.getDoctor({
      pageNum:this.pageNum,
      pageSize:this.pageSize,
      conditions: {
      name:this.name
      }
    })
  },
  mounted() {
  },
  methods:{
    query(){
      this.pageNum=1
      this.getDoctor({
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        conditions: {
          name:this.formInline.name
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delSome(){
      let length=this.multipleSelection.length
      if(length<1){
        this.$message({type:'error',message:'至少选择一条数据'})
        return
      }
      this.$confirm('删除后无法恢复，是否继续？','警告',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:"warning"
      }).then(()=> {
        let ids = ""
        for(let i=0;i<length;i++){
          if(i<length-1){ids+=this.multipleSelection[i].id+","}
          else {ids+=this.multipleSelection[i].id}
        }
        delMore({
          ids
        }).then(res=>{
          if(res.code==200){
            this.$message({type:'success',message:'删除成功'})
            this.getDoctor({
              pageNum:this.pageNum,
              pageSize:this.pageSize,
              conditions: {
                name:this.formInline.name
              }
            })
          }else {
            this.$message({type:'error',message:res.msg})
          }

        }).catch(error=>{})
      }).catch(()=>{
        this.$message({type:'info',message:'已取消删除'})
      })
    },
    //修改单条数据
    cgOne(){
      let length = this.multipleSelection.length

      if(length>1){
        this.$message({type:'error',message:'只能同时修改一条数据！'})
        return
      }
      if(length<1){
        this.$message({type:'error',message:'至少选择一条数据！'})
        return
      }
      this.editForm=this.multipleSelection[0]
      this.$nextTick(()=>{this.$refs.edit.showData()})
      this.editDialogFormVisible=true
    },
    getDoctor(params){
      doctorList(params).then(res=>{
            this.tableData = res.data.list;
            this.total = res.data.total
          }
      ).catch(error=>{})
    },
    handleCurrentChange(val){
      this.pageNum=val;
      this.getDoctor({
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        conditions: {name:this.formInline.name}
      })
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.pageNum=1;
      this.getDoctor({
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        conditions: {name:this.formInline.name}
      })
    },
    adddialog(){
      this.addDialogFormVisible = true;
    },
    delOneRow(row){
      this.$confirm('删除后无法恢复，是否继续？','警告',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:"warning"
      }).then(()=>{
        delOne({id:row.id}).then(res=>{
          if(res.code==200){
            this.$message({type:'success',message:'删除成功'})
            this.getDoctor({
              pageNum:this.pageNum,
              pageSize:this.pageSize,
              conditions: {
                name:this.formInline.name
              }
            })
          }else {
            this.$message({type:'error',message:res.msg})
          }
        })
      }).catch(()=>{
        this.$message({type:'info',message:'已取消删除'})
      })
    },
    editdialog(row){
      this.editForm=row
      this.$nextTick(()=>{
        this.$refs.edit.showData()})
      this.editDialogFormVisible = true
    },
    closeParentEditDialog(){
      this.editDialogFormVisible = false
      this.getDoctor({
        pageNum:this.pageNum,
        pageSize:this.pageSize,
        conditions: {
          name:this.formInline.name
        }})
    }

  }
}
</script>

<style scoped>
.demo-form-inline{

}

</style>
